package com.oa.web.action.base;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.oa.utils.PageBean;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.hibernate.criterion.DetachedCriteria;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class BaseAction<T> extends ActionSupport implements ModelDriven<T>, SessionAware {

    protected final static String HOME = "home";
    protected final static String LOGIN = "login";
    protected final static String LOGOUT = "logout";
    protected final static String LIST = "list";


    // session
    protected Map<String, Object> session;

    protected T model;

    @Override
    public T getModel() {
        return model;
    }

    // 离线查询对象
    private DetachedCriteria dc;

    // PageBean 分页查询对象
    protected PageBean pageBean = new PageBean();

    public BaseAction() {
        // 通过反射创建对象
        ParameterizedType superclass = (ParameterizedType) this.getClass().getGenericSuperclass();
        Type[] tp = superclass.getActualTypeArguments();

        Class<T> mt = (Class<T>) tp[0];
        // 离线查询对象
        dc = DetachedCriteria.forClass(mt);
        pageBean.setCriteria(dc);
        try {
            model = mt.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * 普通对象转 json 并写入客户端
     *
     * @param obj
     * @param excludes
     */
    public void writeObj2Json(Object obj, String[] excludes) {
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(excludes);
        JSONObject jobj = JSONObject.fromObject(obj, jsonConfig);
        // 向客户端写入json
        this.write2Json(jobj.toString());
    }

    /**
     * list 转json 并写入客户端
     *
     * @param list
     * @param excludes
     */
    public void writeList2Json(List list, String[] excludes) {
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(excludes);
        JSONArray jarr = JSONArray.fromObject(list, jsonConfig);
        // 向客户端写入json
        this.write2Json(jarr.toString());
    }

    /**
     * 默认的写入方式
     *
     * @param obj
     */
    public void writeOjb2Json(Object obj) {
        this.writeObj2Json(obj, new String[]{"currentPage", "pageSize", "criteria"});
    }

    /**
     * 向客户端写入 text 普通文本
     *
     * @param text
     */
    public void writeText(String text) {
        String contentType = "text/html;charset=utf-8";
        write(contentType, text);
    }

    /**
     * 向客户端写入 json 数据
     *
     * @param json
     */
    public void write2Json(String json) {
        String contentType = "application/json;charset=utf-8";
        write(contentType, json);
    }

    /**
     * 写入
     *
     * @param contentType
     * @param textORjson
     */
    public void write(String contentType, String textORjson) {
        HttpServletResponse response = ServletActionContext.getResponse();

        response.setContentType(contentType);

        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.write(textORjson);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    public void setSession(Map<String, Object> session) {
        this.session = session;
    }

    // page:2
    // rows:20

    public void setPage(Integer page) {
        pageBean.setCurrentPage(page);
    }

    public void setLimit(Integer limit) {
        pageBean.setPageSize(limit);
    }

}
